﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Collections;
using System.ComponentModel;

namespace LinkViewer.Controls
{
    /// <summary>
    /// Interaction logic for ShowPagesDialog.xaml
    /// </summary>
    public partial class ShowPagesDialog : Window
    {
        ObservableHashSet<string> lst = new ObservableHashSet<string>();
        public ShowPagesDialog(string message, jcc.com.dailyez.Link[] links, string defaultButtonText, string cancelButtonText)
        {
            InitializeComponent();
            jcc.StandardWebService.WebService.GetPageCompleted += new jcc.com.dailyez.GetPageCompletedEventHandler(WebService_GetPageCompleted);
            jcc.StandardWebService.WebService.GetFolderPathCompleted += new jcc.com.dailyez.GetFolderPathCompletedEventHandler(WebService_GetFolderPathCompleted);
            jcc.StandardWebService.WebService.GetBadLinkCompleted += new jcc.com.dailyez.GetBadLinkCompletedEventHandler(WebService_GetBadLinkCompleted);
            lblMessage.Content = message;
            bYes.Content = defaultButtonText;
            bNo.Content = cancelButtonText;
            lbList.Items.Clear();
            lbList.ItemsSource = lst;
            BackgroundWorker worker = new BackgroundWorker();
            worker.DoWork += new DoWorkEventHandler(worker_DoWork);
            worker.RunWorkerAsync(links);
           
        }

        void worker_DoWork(object sender, DoWorkEventArgs e)
        {
            foreach (jcc.com.dailyez.Link link in e.Argument as jcc.com.dailyez.Link[])
            {
                //if the link is suspended get the original page ID instead
                if (link.PageID == App.Suspended_Page_ID)
                {
                    jcc.StandardWebService.WebService.GetBadLinkAsync(jcc.StandardWebService.GetKey(), link.ID); 
                }
                else
                    jcc.StandardWebService.WebService.GetPageAsync(jcc.StandardWebService.GetKey(), link.PageID); 
            }
        }

        void WebService_GetBadLinkCompleted(object sender, jcc.com.dailyez.GetBadLinkCompletedEventArgs e)
        {
            try
            {
                if (e.Result != null)
                    jcc.StandardWebService.WebService.GetPageAsync(jcc.StandardWebService.GetKey(), e.Result.Original_Page_ID);
            }
            catch
            { }
        }

        void WebService_GetFolderPathCompleted(object sender, jcc.com.dailyez.GetFolderPathCompletedEventArgs e)
        {
            try
            {
                this.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, new Action(delegate() { try { lst.Add(e.Result + " / " + e.UserState.ToString()); } catch { } }));
               
 
            }
            catch
            { }
        }

        void WebService_GetPageCompleted(object sender, jcc.com.dailyez.GetPageCompletedEventArgs e)
        {

            //weird stuff, this sometimes will be executed wayy more times than necessary, that's why we're using a hashset
            try
            {
                jcc.StandardWebService.WebService.GetFolderPathAsync(jcc.StandardWebService.GetKey(), e.Result.Folder_ID, e.Result.Title);
            }
            catch
            { }
        }
        protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
        {
      
            
            base.OnClosing(e);
        }
        private void bYes_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = true;
        }

      
    }
}
